package com.yanqu.road.dao.impl.platform;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.platform.ServerMaxOnline;
import com.yanqu.road.entity.platform.ServerRegister;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlatformDaoImpl extends TempDao {

    public ServerMaxOnline getServerMaxOnline(long serverId, long queryStartTime, long queryEndTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT serverId,MAX(userCount)maxCount FROM t_s_online_data WHERE serverId=? AND time>=? AND time<?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, serverId);
            call.setLong(2, queryStartTime);
            call.setLong(3, queryEndTime);
            rs = call.executeQuery();
            while (rs.next()) {
                return initServerMaxOnline(rs);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<Long, ServerMaxOnline> getServerMaxOnlineMap(long queryStartTime, long queryEndTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, ServerMaxOnline> dataMap = new ConcurrentHashMap<>();
        String sql = "SELECT serverId,MAX(userCount)maxCount FROM t_s_online_data WHERE time>=? AND time<? GROUP BY serverId";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, queryStartTime);
            call.setLong(2, queryEndTime);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerMaxOnline data = initServerMaxOnline(rs);
                dataMap.put(data.getServerId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public ServerRegister getServerRegister(long serverId, long queryStartTime, long queryEndTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT serverId,COUNT(userId)ucount FROM t_s_user_list WHERE serverId=? AND registerTime>=? AND registerTime<?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, serverId);
            call.setLong(2, queryStartTime);
            call.setLong(3, queryEndTime);
            rs = call.executeQuery();
            while (rs.next()) {
                return initServerRegister(rs);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<Long, ServerRegister> getServerRegisterMap(long queryStartTime, long queryEndTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, ServerRegister> dataMap = new ConcurrentHashMap<>();
        String sql = "SELECT serverId,COUNT(userId)ucount FROM t_s_user_list WHERE registerTime>=? AND registerTime<? GROUP BY serverId";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, queryStartTime);
            call.setLong(2, queryEndTime);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerRegister data = initServerRegister(rs);
                dataMap.put(data.getServerId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private ServerMaxOnline initServerMaxOnline(ResultSet rs) throws SQLException {
        ServerMaxOnline data = new ServerMaxOnline();
        data.setServerId(rs.getLong("serverId"));
        data.setMaxCount(rs.getInt("maxCount"));
        return data;
    }

    private ServerRegister initServerRegister(ResultSet rs) throws SQLException {
        ServerRegister data = new ServerRegister();
        data.setServerId(rs.getLong("serverId"));
        data.setCount(rs.getInt("ucount"));
        return data;
    }
}
